//
//  HJMakeAppointmentSelectView.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/5/18.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJMakeAppointmentSelectView.h"

#import "HJCommonTableHeaderView.h"
#import "HJMakeAppointmentSelectViewCell.h"

#import "HJNotConmmingPadModel.h"

@interface HJMakeAppointmentSelectView()<UITableViewDelegate,UITableViewDataSource>
/**
 头部
 */
@property(nonatomic,weak)HJCommonTableHeaderView*headerView;
/**
 内容视图
 */
@property(nonatomic,weak)UIView*contentView;
/**
 tableView
 */
@property(nonatomic,strong)UITableView*tableView;
/**
 用户选择的cell
 */
@property(nonatomic,strong)NSIndexPath*selectIndexPath;
@end
@implementation HJMakeAppointmentSelectView


InitFrameM
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        [self.contentView addSubview:_tableView];
        WEAKSELF;
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(weakSelf.contentView);
            make.top.mas_equalTo(weakSelf.headerView.mas_bottom);
            make.bottom.mas_equalTo(weakSelf.contentView).inset(100);
        }];
        
        UIButton*btn=[[UIButton alloc]init];
        [self.contentView addSubview:btn];
        btn.backgroundColor=[UIColor redColor];
        [btn setTitle:@"确 定" forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(80, 35));
            make.right.mas_equalTo(weakSelf.contentView).inset(20);
            make.bottom.mas_equalTo(weakSelf.contentView).inset(20);
        }];
        btn.layer.cornerRadius=5;
        btn.layer.masksToBounds=YES;
        [btn addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tableView;
}
-(void)clickSure{
    if (!self.dataAry.count) {
        return;
    }
    if ([_delegate respondsToSelector:@selector(makeAppointmentSelectView:clickSelctCell:)]) {
        [_delegate makeAppointmentSelectView:self clickSelctCell:self.dataAry[self.selectIndexPath.row]];
    }
    [self removeFromSuperview];
}
-(void)setUI{

    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    WEAKSELF;
    NSMutableArray *labelMutabelArray = [[NSMutableArray alloc]initWithObjects:@"单选框",@"预约单号",@"车牌号",@"VIN",@"预约人",@"预约电话",@"预约进场时间",@"服务顾问", nil];
    UIView*contentView=[[UIView alloc]init];
    contentView.backgroundColor=[UIColor whiteColor];
    [self addSubview:contentView];
    contentView.layer.cornerRadius=8;
    contentView.layer.masksToBounds=YES;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf).inset(30);
        make.center.mas_equalTo(weakSelf);
        make.height.mas_equalTo(400);
    }];
    self.contentView=contentView;
   //头部
    HJCommonTableHeaderView*headerView=[[HJCommonTableHeaderView alloc]init];
    headerView.titleAry=[labelMutabelArray copy];
    self.headerView=headerView;
    [contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(contentView);
        make.height.mas_equalTo(45);
    }];
}
-(void)setDataAry:(NSArray *)dataAry
{
    _dataAry=dataAry;
    [self.tableView reloadData];
    if (dataAry.count) {
        //默认选择第一行
        NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    }
    
}
#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //
    return self.dataAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJMakeAppointmentSelectViewCell*cell=[HJMakeAppointmentSelectViewCell cellWithTable:tableView];
    cell.notConmmingPadModel=self.dataAry[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIndexPath=indexPath;
}
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if(indexPath.row%2 == 1)
//    {
//        cell.backgroundColor = [[UIColor alloc]initWithRed:226.00/255 green:226.00/255 blue:226.00/255 alpha:1];
//    }
//    else
//    {
//        cell.backgroundColor = [UIColor whiteColor];
//    }
//}
@end
